<?php
namespace app\admin\controller;
use think\Controller;

class Base extends Controller
{
	
	//前置操作
    protected function _initialize()
    {
    	if (!session('?ad_name') || !session('?ad_group_id')) {
    		$this->redirect('/admin/login.html');
    	}else{
            $auth = db("ad_group")->where("id = ".session('ad_group_id'))->find();
            $auth = json_decode($auth["auth"],true);
            $controller = strtolower(request()->controller());
            $action = strtolower(request()->action());
            // dump($auth);
            if ($controller != "index" && !isset($auth[$controller.'_'.$action])) {
                if (request()->isAjax()) {
                    $array = ["status" => 0,"message" => "抱歉，权限不足"];
                    header("Content-Type:application/json; charset=UTF-8");
                    echo json_encode($array,JSON_UNESCAPED_UNICODE);
                    exit();
                }
                $this->assign("message","抱歉，权限不足");
                echo $this->fetch("/pub/denied");
                exit();
            }

            $logs["ad_name"] = session("ad_name");
            $logs["ad_id"] = session("ad_id");

            $logs["add_time"] = time();
            $logs["day"] = date("Ymd");
            // $logs["time_info"] = date("Y-m-d H:i:s");
            $logs["query_url"] = $_SERVER["QUERY_STRING"];
            $logs["query_ip"] = $_SERVER["REMOTE_ADDR"];
            $logs["method"] = $_SERVER["REQUEST_METHOD"];
            $logs["url"] = $_SERVER["REQUEST_URI"];
            $logs["get"] = json_encode(input("get."),JSON_UNESCAPED_UNICODE);
            $logs["post"] = json_encode(input("post."),JSON_UNESCAPED_UNICODE);
            $logs["files"] = json_encode($_FILES,JSON_UNESCAPED_UNICODE);
            if (strtoupper($logs["method"]) == "POST") {
                db("ad_log")->insert($logs);
            }else if (strpos($logs["url"], "delete") !== false){
                db("ad_log")->insert($logs);
            }else if (strpos($logs["url"], "copy") !== false){
                db("ad_log")->insert($logs);
            }

    		$this->assign("ad_name",session('ad_name'));
            $this->assign("ad_id",session('ad_id'));
    	}

    }
}
